package com.miniot.environment.config;

import com.alibaba.fastjson.JSON;
import com.miniot.business.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther:lsh
 * @Date: 2022-02-01-14:52
 * @Description: com.miniot.base.config
 * @Version: 1.0
 */
@Slf4j
public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("未认证授权", authException);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(401);
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(R.fail("未认证授权").setCode(401)));
    }
}
